package com.yida.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yida.bean.blogtype;
import com.yida.bean.tag;
import com.yida.serviceAdmin.serverAdminTag;

@Controller
@RequestMapping("admin")
public class tags {
	
	@Autowired
	serverAdminTag serverAdminTag;
	
	  @RequestMapping("/tags")
	  public String tags(Model mode,Integer page) {
		 Integer pages = 1;
			if (page!=null) {
				pages=page;
			}
		Integer   pageall   = serverAdminTag.selectTagSum();
		
		List<tag> taglist = serverAdminTag.selectTags(pages);
		
	    mode.addAttribute("taglist", taglist);
		mode.addAttribute("pages", pageall);
		mode.addAttribute("cpage", pages);
		
		
		return "admin/tags";
		
	}
	  
	  @RequestMapping("/tages/delete")
		public String typesdelete(Model mode,Integer id,RedirectAttributes attributes,Integer page) {
				
		  if (id!=null) {
			                  Integer jie	= serverAdminTag.deleteTag(id);
			                  
			             if (jie>0) {
			            	 attributes.addFlashAttribute("message","删除成功");	               
						}else{
							 attributes.addFlashAttribute("message","无该条数据");
			                 
						}         
				}
				else {
					 attributes.addFlashAttribute("message", "无效操作");   
				}     
		  
		      attributes.addFlashAttribute("page", page);
		      
			 return "redirect:/admin/tags";
		}
	  
	  
	  @RequestMapping("/tags/updateInput")
		public String updateInput(Model mode,Integer id,String name) {	
			 mode.addAttribute("id", id);
			 mode.addAttribute("name", name);   
			 return "admin/tag-input"; 
		}
	  
	  
	  
	  @RequestMapping("/tags/updateTag")
	  public String updateTag(Model mode,tag tag,RedirectAttributes attributes) {
			
			Integer jie = serverAdminTag .updateTag(tag);
			
			   if (jie>0) {
	          	 attributes.addFlashAttribute("message","编辑成功!");
	        	 return "redirect:/admin/tags";
				}else{
				 attributes.addFlashAttribute("message","编辑失败!");
				 return "redirect:/admin/tags";
					  
				}       
		}
	  
	  @RequestMapping("/tags/insertinput")
		public String typesupdateinput(Model mode) {	
			 return "admin/tag-input"; 
		}
	  
	  
	  @RequestMapping("/tags/insert")
		public String tagsinsert(Model mode,tag tag,RedirectAttributes attributes) {
			
			Integer jie = serverAdminTag.insertTag(tag);
			   if (jie>0) {
	          	 attributes.addFlashAttribute("message","新增成功");
	        	 return "redirect:/admin/tags";
				}else{	
					  return "redirect:/admin/tags/insertinput";
				}       
		}
	  
	  

}
